package com.nosql.db.cluster;

import java.io.Serializable;

public class Node implements Serializable {
    public enum Role { LEADER, FOLLOWER }

    private final String id;
    private final String host;
    private final int port;
    private Role role;
    private volatile long lastHeartbeatTime;
    private static final long serialVersionUID = 1L;
    // 新增字段表示是否当前节点
    public Node(String id, String host, int port, Role role) {
        this.id = id;
        this.host = host;
        this.port = port;
        this.role = role;
        this.lastHeartbeatTime = System.currentTimeMillis();
    }

    // 心跳更新时间
    public void updateHeartbeat() {
        this.lastHeartbeatTime = System.currentTimeMillis();
    }

    // 判断节点是否存活
    public boolean isAlive() {
        return System.currentTimeMillis() - lastHeartbeatTime < 3000;
    }

    // Getters & Setters
    public String getId() { return id; }
    public String getHost() { return host; }
    public int getPort() { return port; }
    public Role getRole() { return role; }
    public void setRole(Role role) { this.role = role; }
}
